package cn.bytecollege.filter;


import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Vip;
import cn.bytecollege.service.VipService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 添加功能过滤器
 * @author Mr.East
 */
@WebFilter(filterName = "VipUpdateFilter",servletNames = "VipUpdateController")
public class VipUpdateFilter implements Filter {
    VipService vipService=null;
    public VipUpdateFilter(){
        vipService=new VipService();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String msg = null;
        HttpServletRequest req = (HttpServletRequest) request;
        String method = req.getMethod();


        //复制到下面就500
//        Vip vip=vipService.findByVipId(Integer.valueOf("vipId"));





        if (method.equalsIgnoreCase("post")){
            //获取请求参数
            String vipName = request.getParameter("vipName");
            String  vipId =request.getParameter("vipId");
            String vipGender = request.getParameter("vipGender");
            String vipIdNumber = request.getParameter("vipIdNumber");
            String vipPhoneNumber = request.getParameter("vipPhoneNumber");
            Vip vip=new Vip();
             vip=vipService.findByVipId(Integer.valueOf(vipId));
            request.setAttribute("vip",vip);
            //姓名 /^[\u4e00-\u9fa5]{2,6}$/
            Pattern vipNamePatten = Pattern.compile("^[\\u4e00-\\u9fa5]{2,6}$");
            Matcher vipNameMatcher = vipNamePatten.matcher(vipName);
            if (!vipNameMatcher.matches()){
                msg = "请输入2~6位中文";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher(Resource.PAGE_LOCATION+"/vipUpdate.jsp")
                        .forward(request,response);
                return;
            }
            //性别 /^男$|^女&/
            Pattern vipGenderPatten = Pattern.compile("^男$|^女$");
            Matcher vipGenderMatcher = vipGenderPatten.matcher(vipGender);
            if (!vipGenderMatcher.matches()){
                msg = "从男或女中选择输入";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher(Resource.PAGE_LOCATION+"/vipUpdate.jsp")
                        .forward(request,response);
                return;
            }
            //身份证号 ^\d{18}$
            Pattern vipIdNumberPatten = Pattern.compile("^\\d{18}$");
            Matcher vipIdNumberMatcher = vipIdNumberPatten.matcher(vipIdNumber);
            if (!vipIdNumberMatcher.matches()){
                msg = "请输入正确身份证号";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher(Resource.PAGE_LOCATION+"/vipUpdate.jsp")
                        .forward(request,response);
                return;
            }
            //电话 ^\d{11}$
            Pattern vipPhoneNumberPatten = Pattern.compile("^\\d{11}$");
            Matcher vipPhoneNumberMatcher = vipPhoneNumberPatten.matcher(vipPhoneNumber);
            if (!vipPhoneNumberMatcher.matches()){
                msg = "请输入正确的电话";
                request.setAttribute("msg",msg);
                request.getRequestDispatcher(Resource.PAGE_LOCATION+"/vipUpdate.jsp")
                        .forward(request,response);
                return;
            }
            chain.doFilter(request, response);
        }else{
            chain.doFilter(request,response);
        }
    }
}
